package com.gqz.patrol.app.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.patrol.app.entity.EnPatrolTaskPeriodEntity;
import com.gqz.patrol.app.mapper.EnPatrolTaskPeriodMapper;
import com.gqz.patrol.app.mapstruct.EnPatrolTaskPeriodMapstruct;
import com.gqz.patrol.app.service.IEnPatrolTaskPeriodService;
import com.gqz.patrol.vo.EnPatrolTaskPeriodVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 能源-巡视工单_周期任务表 ҵ���߼�ʵ�ֲ�ServiceImpl
 */
@Service
@Slf4j
public class EnPatrolTaskPeriodServiceImpl extends ServiceImpl<EnPatrolTaskPeriodMapper, EnPatrolTaskPeriodEntity> implements IEnPatrolTaskPeriodService {

    @Autowired
    private EnPatrolTaskPeriodMapstruct mapstruct;

    @Override
    public List<EnPatrolTaskPeriodVo> getById(String id) {
        MPJLambdaWrapper<EnPatrolTaskPeriodEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnPatrolTaskPeriodEntity::getId, id);
        queryWrapper.selectAll(EnPatrolTaskPeriodEntity.class);
        List<EnPatrolTaskPeriodEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnPatrolTaskPeriodVo> page(RequestForm<EnPatrolTaskPeriodVo> requestForm) {
        EnPatrolTaskPeriodEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnPatrolTaskPeriodEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnPatrolTaskPeriodEntity.class);
        Page<EnPatrolTaskPeriodEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnPatrolTaskPeriodEntity> iPage = baseMapper.selectJoinPage(page, EnPatrolTaskPeriodEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnPatrolTaskPeriodVo> requestForm) {
        EnPatrolTaskPeriodEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnPatrolTaskPeriodEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must�޸�Ϊ��¼�������ֶ�todo */
        queryWrapper.select(EnPatrolTaskPeriodEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnPatrolTaskPeriodVo> forms) {
        List<EnPatrolTaskPeriodEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnPatrolTaskPeriodEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnPatrolTaskPeriodVo> forms) {
        List<EnPatrolTaskPeriodEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnPatrolTaskPeriodEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    public List<EnPatrolTaskPeriodVo> getPatrolPeriodTaskList(int offsetDay) {
        QueryWrapper<EnPatrolTaskPeriodEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EnPatrolTaskPeriodEntity::getExecuteStatus, "0");
        List<EnPatrolTaskPeriodEntity> taskPeriodEntities = baseMapper.selectList(queryWrapper);
        List<EnPatrolTaskPeriodVo> taskPeriodVos = new ArrayList<>();
        taskPeriodEntities.forEach(taskPeriodEntity -> {
            String executeStime = DateUtil.formatDate(taskPeriodEntity.getExecuteStime());
            if (DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(DateUtil.today()), offsetDay)).equals(executeStime)
                    && taskPeriodEntity.getExecuteStime().before(taskPeriodEntity.getPeriodEndTime())) {
                taskPeriodVos.add(mapstruct.toVo(taskPeriodEntity));
            }
        });
        return taskPeriodVos;
    }


}
